

package com.google.server;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.TimerTask;

import com.dbmanager.DBManager;
import com.pool.BatchSaveImpl;

public class MiDuTimer extends TimerTask
{

	public MiDuTimer()
	{
	}

	public void run()
	{
		judgeMiDuAlarm();
	}

	public void judgeMiDuAlarm()
	{
		PreparedStatement pstam = null;
		Connection conn=null;
		String sb ="select tt.*, vv.vehicle_num,to_char(tt.gpsTime,'yyyy-mm-dd hh24:mi:ss' ) gps_Time\t"
                +"  from g_vehicle vv,"
		        +"       (select *\t"
		        +"          from (select m.*\t"
		        +"                  from (select t.*, (sysdate - t.gpstime) * 24 * 60 time\t"
		        +"                          from g_last_point t) m\t"
		        +"                 where m.time <= 10) aa\t"
		        +"         where (select count(*)\t"
		        +"                  from (select m.*\t"
		        +"                          from (select t.*,\t"
		        +"                        (sysdate - t.gpstime) * 24 * 60 time\t"
		        +"                                  from g_last_point t) m\t"
		        +"                         where m.time <= 10)\t"
		        +"                 where grid_x = aa.grid_x\t"
		        +"                   and grid_y = aa.grid_y) > 1\t"
		        +"         order by aa.grid_x) tt\t"
		        +" where vv.device_id = tt.device_id\t";
		try
		{
			DBManager dbmanager = DBManager.getInstance();
			conn = dbmanager.getConnection();
			pstam = conn.prepareStatement(sb.toString(), 1004, 1007);
			ResultSet rs = pstam.executeQuery();
			String x = null;
			String y = null;
			boolean flag = true;
			String vm = "";
			BatchSaveImpl bsi = new BatchSaveImpl();
			while (rs.next()) 
			{
				String grid_x = rs.getString("grid_x");
				String grid_y = rs.getString("grid_y");
				String vehicle_num = rs.getString("vehicle_num");
				if (flag)
				{
					x = grid_x;
					y = grid_y;
					vm = vehicle_num;
				} else
				if (x.equals(grid_x) && y.equals(grid_y))
				{
					vm = String.valueOf(vm)+","+vehicle_num;
					bsi.saveAlarmToDb(rs.getString("device_id"), rs.getDouble("lng"), rs.getDouble("lat"), rs.getInt("speed"), rs.getString("gps_time"), 9, "车辆密度过密报警", String.valueOf(vm)+"距离过近", "");
				} else
				{
					System.out.println(vm);
					x = grid_x;
					y = grid_y;
					vm = vehicle_num;
				}
				if (rs.isLast())
					bsi.saveAlarmToDb(rs.getString("device_id"), rs.getDouble("lng"), rs.getDouble("lat"), rs.getInt("speed"), rs.getString("gps_time"), 9, "车辆密度过密报警", String.valueOf(vm)+"距离过近", "");
				flag = false;
			}
			
		}
		catch (Exception e)
		{
			e.printStackTrace();
		}finally{
			try {        		
    			if(pstam!=null){
    				
    				pstam.close();
    			}
    			if(conn!=null){
    				conn.close();
    			}
    			
			  } catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
	
		}
}
	}
